package it.gameplanet.client.widgets.support;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;

public class Dialogs {

	private final static DialogBox dialogBox = new DialogBox();
	
	private static final HTML primaryMessageLabel = new HTML();
	private final static Label primaryMessage = new Label();	

	private static final HTML secondayMessageLabel = new HTML();
	private final static HTML secondaryMessage = new HTML();
	
	private final static Button closeButton = new Button("Close");
	
	static {
		createErrorDialogBox();
	}

	private static void createErrorDialogBox() {
		// Create the popup dialog box
		dialogBox.setText("Errore Applicativo");
		dialogBox.setAnimationEnabled(true);

		// We can set the id of a widget by accessing its Element
		closeButton.getElement().setId("closeButton");
		VerticalPanel dialogVPanel = new VerticalPanel();
		dialogVPanel.addStyleName("dialogVPanel");
		dialogVPanel.add(primaryMessageLabel);
		dialogVPanel.add(primaryMessage);
		dialogVPanel.add(secondayMessageLabel);
		dialogVPanel.add(secondaryMessage);
		dialogVPanel.setHorizontalAlignment(VerticalPanel.ALIGN_RIGHT);
		dialogVPanel.add(closeButton);
		dialogBox.setWidget(dialogVPanel);

		// Add a handler to close the DialogBox
		closeButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				dialogBox.hide();
			}
		});
	}
	
	public static void showException(Throwable caught) {
		GWT.log("showException", caught);
		
		
		primaryMessageLabel.setHTML("<b>Errore:</b>");
		primaryMessage.setText(caught.getMessage());

		secondayMessageLabel.setHTML("<br><b>Dettaglio:</b>");
		secondaryMessage
				.addStyleName("serverResponseLabelError");

		secondaryMessage.setHTML("<pre>"+caught.getLocalizedMessage()+"</pre>");
		
		dialogBox.center();
		closeButton.setFocus(true);
	}

	public static void showMessage(String message) {
		
		// Show the RPC error message to the user
		dialogBox
				.setText("Messaggio");
		secondaryMessage.setHTML("<b>"+message+"</b>");
		dialogBox.center();
		closeButton.setFocus(true);
	}
}
